package be.jaisy.ui.gwt.widget.sample.client;

import be.jaisy.ui.gwt.widget.client.sink.Sink;
import be.jaisy.ui.gwt.widget.client.sink.SinkList;
import be.jaisy.ui.gwt.widget.client.sink.Sink.SinkInfo;
import be.jaisy.ui.gwt.widget.sample.client.table.TableViewerSampleSink;
import be.jaisy.ui.gwt.widget.sample.client.table.TableViewerSimpleSampleSink;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * OSGI UI Console
 */
public class JaisyWidgetSample implements EntryPoint, HistoryListener {

	/**
	 * An image provider to make available images to Bundle.
	 */
	public interface Images extends ImageBundle, TreeImages {
		AbstractImagePrototype star();
	}
	
	protected SinkList list = new SinkList(((JaisyWidgetSample.Images)GWT.create(JaisyWidgetSample.Images.class)).star().createImage());

	private SinkInfo curInfo;

	private Sink curSink;

	private HTML description = new HTML();

	private VerticalPanel panel = new VerticalPanel();

	public void onHistoryChanged(String token) {
		SinkInfo info = list.find(token);
		if (info == null) {
			showInfo();
			return;
		}
		show(info, false);
	}

	public void onModuleLoad() {
		// Load all the sinks.
		loadSinks();

		panel.add(list);
		panel.add(description);
		panel.setWidth("100%");

		description.setStyleName("ks-Description");

		History.addHistoryListener(this);
		RootPanel.get().add(panel);

		// Show the initial screen.
		String initToken = History.getToken();
		if (initToken.length() > 0) {
			onHistoryChanged(initToken);
		} else {
			showInfo();
		}
	}

	public void show(SinkInfo info, boolean affectHistory) {
		if (info == curInfo) {
			return;
		}
		curInfo = info;

		// Remove the old sink from the display area.
		if (curSink != null) {
			curSink.onHide();
			panel.remove(curSink);
		}

		// Get the new sink instance, and display its description in the
		// sink list.
		curSink = info.getInstance();
		list.setSinkSelection(info.getName());
		description.setHTML(info.getDescription());

		// If affectHistory is set, create a new item on the history stack. This
		// will ultimately result in onHistoryChanged() being called. It will
		// call
		// show() again, but nothing will happen because it will request the
		// exact
		// same sink we're already showing.
		if (affectHistory) {
			History.newItem(info.getName());
		}

		// Change the description background color.
		DOM.setStyleAttribute(description.getElement(), "backgroundColor", info.getColor());

		// Display the new sink.
		curSink.setVisible(false);
		panel.add(curSink);
		panel.setCellHorizontalAlignment(curSink, HasHorizontalAlignment.ALIGN_CENTER);
		curSink.setVisible(true);
		curSink.onShow();
	}

	/**
	 * Adds all sinks to the list. Note that this does not create actual
	 * instances of all sinks yet (they are created on-demand). This can make a
	 * significant difference in startup time.
	 */
	protected void loadSinks() {
		list.addSink(TableViewerSimpleSampleSink.init());
		list.addSink(TableViewerSampleSink.init());
	}

	private void showInfo() {
		show(list.find("TableViewerSimpleSampleSink"), false);
	}
}
